﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace kiosk
{
    public partial class GobalMessages : Form
    {
        private List<Item> m_Items;
        private Int32 m_msgindex= 0;
        public GobalMessages()
        {
            InitializeComponent();
           
        }

        private void GobalMessages_Load(object sender, EventArgs e)
        {

            
            this.refreshData();

        }

        private void preBtn_Click(object sender, EventArgs e)
        {
            if (this.m_msgindex > 0)
            {
                this.saveBtn.Text = "Update";
                this.m_msgindex -= 1;
                this.msgTxt.Text = this.m_Items[m_msgindex ].ItemText;
            
            }



        }

        private void nextBtn_Click(object sender, EventArgs e)
        {

            if (this.m_msgindex < this.m_Items.Count -1)
            {
                this.saveBtn.Text = "Update";
                this.m_msgindex += 1;
                this.msgTxt.Text = this.m_Items[m_msgindex ].ItemText;

            }



        }

        private void deleteBtn_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("This Will Delete the Message !! Continue ?? ", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {

                try
                {
                    DataBaseHandler handler = new DataBaseHandler();
                    handler.delGBMessage(this.m_Items[this.m_msgindex].ItemID);
                    this.refreshData();
                    this.saveBtn.Text = "Update";
                    MessageBox.Show("Message Deleted");

                }
                catch(Exception ex)
                {

                    MessageBox.Show("There Was an error" + ex.Message);
                }
            
            
            }





        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            DataBaseHandler handler = new DataBaseHandler();
            handler.parpreConn();
            if(this.msgTxt.Text.Trim()== String.Empty)
            {

                MessageBox.Show("A tricker message cannot be empty !!!");
                return;
            }
           

            try
            {


                if (this.saveBtn.Text == "Update")
                {
                   
                    handler.delGBMessage(this.m_Items[this.m_msgindex].ItemID);
                    this.m_Items.RemoveAt(this.m_msgindex - 1);
                    handler.insertGBMessage(this.msgTxt.Text);
                    this.refreshData();

                    MessageBox.Show("  Message Saved!!");
                }
                else { 
                // a new message must be inserted
                    
                    handler.insertGBMessage(this.msgTxt.Text);
                    this.refreshData();
                    MessageBox.Show("Tricker Message Saved");
                    this.saveBtn.Text = "Update";

                
                
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("There Was an error" + ex.Message);
            }





        }

        private void refreshData()
        {

            try
            {
                DataBaseHandler handler = new DataBaseHandler();
                if(this.m_Items != null)
                   this.m_Items.Clear();

                this.m_Items = handler.getGMSG();
                this.m_msgindex = 0;
                this.msgTxt.Text = this.m_Items[m_msgindex].ItemText;





            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error n the application" + ex.Message);

            }

        
        }

        private void newBtn_Click(object sender, EventArgs e)
        {
            this.saveBtn.Text = "Save";
            this.msgTxt.Text = "";



        }

      


       
    }
}
